package Model;

/*CREATE  TABLE IF NOT EXISTS `mydb`.`Laboratorio` (
  `idLaboratorio` INT NULL AUTO_INCREMENT ,
  `Sala_idSala` INT NOT NULL ,
  `Propositos_idPropositos` INT NOT NULL ,
  `nome` VARCHAR(100) NULL ,
  `descricao` VARCHAR(250) NULL ,
  PRIMARY KEY (`idLaboratorio`) ,
  INDEX `fk_Laboratorio_Sala1` (`Sala_idSala` ASC) ,
  INDEX `fk_Laboratorio_Propositos1` (`Propositos_idPropositos` ASC) ,
  CONSTRAINT `fk_Laboratorio_Sala1`
    FOREIGN KEY (`Sala_idSala` )
    REFERENCES `mydb`.`Sala` (`idSala` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Laboratorio_Propositos1`
    FOREIGN KEY (`Propositos_idPropositos` )
    REFERENCES `mydb`.`Propositos` (`idPropositos` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;*/
public class Laboratorio {
    //<editor-fold defaultstate="collapsed" desc="Connection">
    private static com.mysql.jdbc.Connection connection;

    public static com.mysql.jdbc.Connection getConnection() {
        return connection;
    }

    public static void setConnection(com.mysql.jdbc.Connection connection) {
        Laboratorio.connection = connection;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Variable Declaration">
    private int idLaboratorio;
    private Sala Sala_idSala;
    private Propositos Propositos_idPropositos;
    private String nome;
    private String descricao;
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Constructor">
    public Laboratorio(int idLaboratorio) {
        try {
            com.mysql.jdbc.ResultSet rs = (com.mysql.jdbc.ResultSet) connection.createStatement().executeQuery("SELECT * FROM itens WHERE idLaboratorio = '" + idLaboratorio + "'");
            rs.next();
            this.idLaboratorio = rs.getInt("idLaboratorio");
            this.Sala_idSala = new Sala(rs.getInt("Sala_idSala"));
            this.Propositos_idPropositos = new Propositos(rs.getInt("Propositos_idPropositos"));
            this.nome = rs.getString("nome");
            this.descricao = rs.getString("descricao");
        } catch (Exception e) {
        }
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="toString">
    @Override
    public String toString() {
        String s="Laboratorio:";
            s += "idLaboratorio=" + idLaboratorio;
        return s;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Getter">
    public int idLaboratorio() {
        return this.idLaboratorio;
    }

    public Sala Sala_idSala() {
        return this.Sala_idSala;
    }

    public Propositos Propositos_idPropositos() {
        return this.Propositos_idPropositos;
    }

    public String nome() {
        return this.nome;
    }

    public String descricao() {
        return this.descricao;
    }

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Setter">
    public boolean idLaboratorio(int idLaboratorio) {
        boolean ok = update("idLaboratorio", idLaboratorio);
        if (ok) {
            this.idLaboratorio = idLaboratorio;
        }
        return ok;
    }
    public boolean Sala_idSala(Sala Sala_idSala) {
        boolean ok = update("Sala_idSala", Sala_idSala.idSala());
        if (ok) {
            this.Sala_idSala = Sala_idSala;
        }
        return ok;
    }
    public boolean Propositos_idPropositos(Propositos Propositos_idPropositos) {
        boolean ok = update("Propositos_idPropositos", Propositos_idPropositos.idPropositos());
        if (ok) {
            this.Propositos_idPropositos = Propositos_idPropositos;
        }
        return ok;
    }
    public boolean nome(String nome) {
        boolean ok = update("nome", nome);
        if (ok) {
            this.nome = nome;
        }
        return ok;
    }
    public boolean descricao(String descricao) {
        boolean ok = update("descricao", descricao);
        if (ok) {
            this.descricao = descricao;
        }
        return ok;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Update">
    /**
     *
     * @param campo
     * @param valor
     * @return
     */
    public boolean update(String campo, Object valor) {
        try {
            String sql = "UPDATE Laboratorio \n"
                    + "SET " + campo + " = '";
            if (valor instanceof String) {
                sql += (String) valor;
            } else if (valor instanceof Integer) {
                sql += (Integer) valor;
            } else if (valor instanceof java.sql.Timestamp) {
                sql += ((java.sql.Timestamp) valor).toString().split("\\.")[0];
            } else if (valor instanceof java.util.Date) {
                java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                        "yyyy-MM-dd hh:mm:ss");
                sql += sdf.format((java.util.Date) valor);
            } else {
                sql += valor.toString();
            }
            sql += "'\n WHERE \n idLaboratorio = '" + idLaboratorio + "'";
            com.mysql.jdbc.Statement st =
                    (com.mysql.jdbc.Statement) connection.createStatement();
            int x = st.executeUpdate(sql);
            if (x == 1) {
                return true;
            }
        } catch (Exception ex) {
        }
        return false;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Search">
    public static java.util.ArrayList<Laboratorio> searchIdLaboratorio(int valor) {
        return search("idLaboratorio", valor+"");
    }
    public static java.util.ArrayList<Laboratorio> searchSala_idSala(Sala valor) {
        return search("Sala_idSala", valor.idSala()+"");
    }
    public static java.util.ArrayList<Laboratorio> searchPropositos_idPropositos(Propositos valor) {
        return search("Propositos_idPropositos", valor.idPropositos()+"");
    }
    public static java.util.ArrayList<Laboratorio> searchNome(String valor) {
        return search("nome", valor+"");
    }
    public static java.util.ArrayList<Laboratorio> searchDescricao(String valor) {
        return search("descricao", valor+"");
    }

    public static java.util.ArrayList<Laboratorio> search(String campo, String valor) {
        try {
            java.util.ArrayList<Laboratorio> retorno = new java.util.ArrayList<>();
            String sql = "SELECT idLaboratorio FROM Laboratorio "
                    + "WHERE " + campo + " LIKE '%" + valor + "%' "
                    + "ORDER BY " + campo;
            com.mysql.jdbc.ResultSet rs = (com.mysql.jdbc.ResultSet) connection.createStatement().executeQuery(sql);
            while (rs.next()) {
                retorno.add(new Laboratorio(rs.getInt("idLaboratorio")));
            }
            return retorno;
        } catch (Exception e) {
        }
        return null;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="New">
    public static Laboratorio newLaboratorio(int idLaboratorio, Sala Sala_idSala, Propositos Propositos_idPropositos, String nome, String descricao){
            String sql = "INSERT INTO Laboratorio (idLaboratorio, Sala_idSala, Propositos_idPropositos, nome, descricao)"
                    + " VALUES "
                    + "('"+idLaboratorio+"', '"+Sala_idSala.idSala() + "', '"+Propositos_idPropositos.idPropositos() + "', '"+nome+"', '"+descricao+"');";
        com.mysql.jdbc.Statement stmt = null;
        com.mysql.jdbc.ResultSet rs = null;
        try {
            stmt = (com.mysql.jdbc.Statement) connection.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
                    java.sql.ResultSet.CONCUR_UPDATABLE);
            stmt.executeUpdate(sql, com.mysql.jdbc.Statement.RETURN_GENERATED_KEYS);
            rs = (com.mysql.jdbc.ResultSet) stmt.getGeneratedKeys();
            if (rs.next()) {
                return new Laboratorio(rs.getInt("idLaboratorio"));
            } else {
                return new Laboratorio(idLaboratorio);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception ex) {
                    // ignore
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception ex) {
                    // ignore
                }
            }
        }
        return null;
    }
    //</editor-fold>
}